function wxUploadFile(
    {
        uploadApiUrl = "/File/uploadWxImage",
        maxCount = 1,
        loading = true,
        sizeType = ['original', 'compressed'],
        sourceType = ['album', 'camera'],
        otherParam = {},
        callback = Function()
    }
) {
    let $loadingWidget = null;

    if (!wx) {
        return $.toast("请加载微信环境");
    }

    const {chooseImage, uploadImage} = wx;

    chooseImage({
        count: maxCount,
        sizeType,
        sourceType,
        success({localIds = []}) {
            if (!localIds.length) {
                return;
            }

            if (loading) {
                if ($loadingWidget) {
                    $loadingWidget.remove();
                }
                $loadingWidget = $.loading("上传中")
            }

            upload({
                localIds,
                callback(res) {
                    callback(res);
                    if ($loadingWidget) {
                        $loadingWidget.remove();
                    }
                }
            })
        }
    });

    function upload(
        {
            localIds = [],
            callback = Function()
        }
    ) {
        wxBatchUploadImage({
            localIds,
            callback(serverIds = []) {
                if (!serverIds.length) {
                    return callback({
                        Code: -10000,
                        Message: "没有任何文件被上传"
                    });
                }
                uploadApi({
                    ...otherParam,
                    serverIds: serverIds.join(",")
                }, res => callback(res))
            }
        })
    }

    function wxBatchUploadImage(
        {
            localIds = [],
            serverIds = [],
            callback = Function()
        }
    ) {
        if (!localIds.length) {
            callback(serverIds);
        }

        const localId = localIds.shift();
        uploadImage({
            localId,
            isShowProgressTips: 0,
            success({serverId}) {
                if (serverId) {
                    serverIds.push(serverId);
                }
                wxBatchUploadImage({
                    localIds,
                    serverIds,
                    callback
                });
            }
        })


    }

    function uploadApi(data, callback = Function()) {
        return $.sApi({
            data,
            url: uploadApiUrl
        }, callback)
    }
}